package com.yinhai.service;


import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import com.yinhai.dao.SMSDaoImpl;
import com.yinhai.model.MessageBean;
import com.yinhai.model.SMSBean;

import android.content.ContentResolver;
import android.content.Context;
import android.database.Cursor;
import android.net.Uri;

/**
 * 短信操作类
 * @author Administrator
 *
 */
public class SMSServiceImpl {
	SMSDaoImpl rsms;
	private SimpleDateFormat sdf;
	private Context context;
	public SMSServiceImpl(Context context) {
		rsms = new SMSDaoImpl(context);
		sdf = new SimpleDateFormat("MM-dd HH:mm");
		this.context = context;
	}
	
	/**
	 * 获取短信列表
	 * @return
	 */
	/*public List<SMSBean> find() {
		return rsms.getThreadsNum(rsms.getThreads(0));
	}*/
	public List<SMSBean> findPage(int pageNo){
		
		return  rsms.getThreadsPage(pageNo);
//		return  rsms.getThreadsNum(rsms.getThreadsPage(pageNo));
	}
	public int findThreadCount(){
		return rsms.getThreads(0).size();
	}
	public int findMsgCount(){
		return rsms.getMsgCount();
	}
	/**
	 * 点短信进入的消息列表
	 * @param thread_id短信的唯一标识
	 * @return
	 */
	public List<MessageBean> findMessage(String thread_id) {
		List<MessageBean> messages = new ArrayList<MessageBean>();
		Cursor cursor = null;
		ContentResolver contentResolver = context.getContentResolver();
		Uri uri = Uri.parse("content://sms");
		String[] projection = new String[] { "date", "address", "person",
				"body", "type" }; // 查询的列
		cursor = contentResolver.query(uri, projection, "thread_id = " + thread_id, null, "date asc");
		if (cursor != null && cursor.getCount() > 0) {
			cursor.moveToFirst();
			for (int i = 0; i < cursor.getCount(); i++) {
				cursor.moveToPosition(i);
				String date = sdf.format(new Date(cursor.getLong(cursor
						.getColumnIndex("date"))));
				if (cursor.getInt(cursor.getColumnIndex("type")) == 1) {
					MessageBean d = new MessageBean(
							cursor.getString(cursor
									.getColumnIndex("address")),
							date,
							cursor.getString(cursor.getColumnIndex("body")),1);
					messages.add(d);
				} else {
					MessageBean d = new MessageBean(
							cursor.getString(cursor
									.getColumnIndex("address")),
							date,
							cursor.getString(cursor.getColumnIndex("body")),2);
					messages.add(d);
				}
			}
		
		}
		if(!cursor.isClosed()){
			cursor.close();
		}
		return messages;
	}
	
}
